package com.moter.demo01.requirements.dao;

import com.moter.demo01.requirements.Demo2;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OrderDao {
    private Demo2 demo2 = null;

    public OrderDao() {
        this.demo2 = new Demo2();
    }

    /**
     * 下单操作
     */
    public Boolean giveOrder(String id) {
        Connection connection = demo2.connect(Demo2.url, Demo2.username, Demo2.password);
        PreparedStatement preparedStatement=null;
        try {
            preparedStatement = connection.prepareStatement("insert into _order values (?)");
            preparedStatement.setString(1,id);
            boolean execute = preparedStatement.execute();
            return execute;
        } catch (SQLException throwable) {
            throwable.printStackTrace();
        } finally {

            try {
                if (preparedStatement != null)
                    preparedStatement.close();
                if (connection!=null)
                    connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }
        }
        return false;
    }

    public List<String> searchOrder() {
        Connection connection = demo2.connect(Demo2.url, Demo2.username, Demo2.password);
        PreparedStatement preparedStatement=null;
        ResultSet resultSet = null;
        ArrayList<String> ids = new ArrayList<>();
        try {
            preparedStatement = connection.prepareStatement("select id from _order");
            resultSet = preparedStatement.executeQuery();
            while(resultSet.next()){
                ids.add(resultSet.getString(1));
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                if(resultSet!=null)
                    resultSet.close();
                if(preparedStatement!=null)
                    preparedStatement.close();
                if (connection!=null)
                    connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally {
                return ids;
            }
        }
    }
    public boolean cancerOrder(String id){
        Connection connection = demo2.connect(Demo2.url, Demo2.username, Demo2.password);
        PreparedStatement preparedStatement=null;
        boolean execute=false;
        try {
            preparedStatement = connection.prepareStatement("delete from _order where id = ?");
            preparedStatement.setString(1,id);
            execute= preparedStatement.execute();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }finally {
            try {
                if(preparedStatement!=null)
                    preparedStatement.close();
                if (connection!=null)
                    connection.close();
            } catch (SQLException throwables) {
                throwables.printStackTrace();
            }finally {
                return execute;
            }
        }


    }
}
